package com.roadmap.ajax.proxy;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.directwebremoting.WebContextFactory;

import com.roadmap.biz.common.IManager;
import com.roadmap.biz.common.ManagerFactory;
import com.roadmap.common.BaseEntity;
import com.roadmap.common.RdcConst;
import com.roadmap.common.RdcErrorMessage;
import com.roadmap.common.RdcLogger;
import com.roadmap.entities.Comment;
import com.roadmap.exception.InvalidValueException;
import com.roadmap.exception.ServerException;
import com.roadmap.struts.action.ActionConst;

public abstract class BaseProxy {
	protected final static Logger _log = RdcLogger.getLogger(BaseProxy.class);
	private final HttpSession _session = WebContextFactory.get().getSession();
	private IManager _bizManager = null;

	protected HttpSession getSession() {
		return _session;
	}

	protected String getUIDInSession() {
		if (getSession() != null) {
			return (String) getSession().getAttribute(
					ActionConst.Aspirant_Id_In_Session);
		}
		return null;
	}

	protected IManager getManager(String type, String parentClazz)
			throws InvalidValueException, ServerException {
		if (_bizManager == null) {
			_bizManager = ManagerFactory.getInstance(type, getUIDInSession(),
					getUIDInSession(), parentClazz);
		}

		return _bizManager;
	}

	protected IManager getManager(String type) throws InvalidValueException,
			ServerException {
		if (_bizManager == null) {
			_bizManager = ManagerFactory.getInstance(type, getUIDInSession());
		}

		return _bizManager;
	}

	protected IManager getManager(BaseEntity entity) throws InvalidValueException,
			ServerException {
		if (_bizManager == null) {
			if (entity instanceof Comment) {
				_bizManager = getManager(entity.getClass().getSimpleName(),
						((Comment) entity).getEntryType());
			}
			else {
				_bizManager = getManager(entity.getClass().getSimpleName());
			}
		}

		return _bizManager;
	}

	protected Object create(BaseEntity entity) {
		if (entity == null) {
			_log.warn("NULL entity was transferred.");
			return RdcErrorMessage.BAD_PARAMETERS;
		}
		try {
			IManager mgr = getManager(entity);
			mgr.create(entity);
			return entity;
		}
		catch (Exception e) {
			return RdcErrorMessage.getMessageToClient(e);
		}
	}

	protected Object update(BaseEntity entity) {
		if (entity == null) {
			_log.warn("NULL entity was transferred.");
			return RdcErrorMessage.BAD_PARAMETERS;
		}

		try {
			IManager mgr = getManager(entity);
			mgr.edit(entity);
			return entity;
		}
		catch (Exception e) {
			return RdcErrorMessage.getMessageToClient(e);
		}
	}

	protected Object delete(String typeSimpleName, String entityId) {
		try {
			IManager mgr = getManager(typeSimpleName);
			mgr.delete(entityId);
			return true;
		}
		catch (Exception e) {
			return RdcErrorMessage.getMessageToClient(e);
		}
	}

	protected Object complete(String typeSimpleName, String entityId) {
		if (typeSimpleName == null || entityId == null) {
			return RdcErrorMessage.BAD_PARAMETERS;
		}

		try {
			IManager mgr = getManager(typeSimpleName);
			return mgr.complete(entityId);
		}
		catch (Exception e) {
			return RdcErrorMessage.getMessageToClient(e);
		}
	}

	protected Object cancel(String typeSimpleName, String entityId) {
		if (entityId == null) {
			return RdcErrorMessage.BAD_PARAMETERS;
		}

		try {
			IManager mgr = getManager(typeSimpleName);
			return mgr.cancel(entityId);
		}
		catch (Exception e) {
			return RdcErrorMessage.getMessageToClient(e);
		}
	}
}
